package com.ruoyi.business.controller;

import com.ruoyi.business.domain.ChapterInfo;
import com.ruoyi.business.domain.bo.ChapterInfoBo;
import com.ruoyi.business.service.IChapterInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.vo.InnerObjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/31
 **/
@RestController
@RequestMapping("/business/chapterInfo")
public class ChapterInfoController extends BaseController {

    @Autowired
    private IChapterInfoService chapterInfoService;

    @GetMapping("/list")
    public AjaxResult chapterInfoList(ChapterInfoBo bo) {
        return AjaxResult.success(chapterInfoService.getChapterInfoList(bo));
    }

    @PostMapping
    public AjaxResult addChapterInfo(@RequestBody @Validated(value = {AddGroup.class}) ChapterInfoBo bo) {
        String id = chapterInfoService.insertChapterInfo(bo);
        if (null != id) {
            return AjaxResult.success(id);
        }else {
            return AjaxResult.error();
        }
    }

    @PutMapping
    public AjaxResult updateChapterInfo(@RequestBody @Validated(value = {EditGroup.class}) ChapterInfoBo bo) {
        Boolean ans = chapterInfoService.updateChapterInfo(bo);
        if (ans) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/move/up/{id}")
    public AjaxResult moveUp(@NotNull(message = "id不能为空")
                             @PathVariable("id") String id) {
        chapterInfoService.moveUp(id);
        return AjaxResult.success();
    }

    @GetMapping("/move/down/{id}")
    public AjaxResult moveDown(@NotNull(message = "id不能为空")
                             @PathVariable("id") String id) {
        chapterInfoService.moveDown(id);
        return AjaxResult.success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult deleteChapterInfo(@NotEmpty(message = "主键不能为空")
                                        @PathVariable Long[] ids) {
        Boolean ans = chapterInfoService.deleteChapterInfo(Arrays.asList(ids), false);
        if (ans) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取第二部确定的章节数
     * @return
     */
    @GetMapping("/chapter/ref")
    public AjaxResult queryChapterInfoRef(@RequestParam(value = "courseId", required = false) String courseId) {
        List<InnerObjVo> list = chapterInfoService.queryChapterInfoRef(courseId);
        return AjaxResult.success(list);
    }

//    /**
//     * 新的组件方法，获取章节的对应关系
//     * @param teacherId
//     * @return
//     */
//    @Log(title = "课程信息", businessType = BusinessType.OTHER)
//    @GetMapping("/ref/teacherId")
//    public AjaxResult queryRefCourseInfo(@RequestParam(value = "teacherId") Long teacherId) {
//        List<InnerObjVo> list = tCourseInfoService.queryRefCourseInfo(teacherId);
//        return AjaxResult.success(list);
//    }
}
